﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebComponents.Data;
using WebComponents;
using WebComponents.Security;
using GBIT.Web;
using System.Collections.Generic;

public partial class SanPhamList : UserControlBase {
  protected void Page_Load(object sender, EventArgs e) {
    this.AddModule = "SanPham/ThemSanPham.ascx";
    this.Title = "Danh sách sản phẩm";
    if (!Page.IsPostBack) {
      LoadCategory();
      LoadSanPham();
    }    
    this.AddEnabled = true;    
  }
  protected void grvSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e) {
    grvSanPham.PageIndex = e.NewPageIndex;
    LoadSanPham();
  }
  protected void LoadCategory() {
    this.cmbNhomSanPham.Items.Clear();
    this.cmbNhomSanPham.Items.Add(new ListItem("-- Tất cả --", "0"));
    Global.LoadCategory(cmbLanguage.SelectedValue, ref cmbNhomSanPham, CatetoryType.Product);
  }
  protected override void OnDeleteRecord() {
    foreach (GridViewRow row in grvSanPham.Rows) {
      if (((HtmlInputCheckBox)row.FindControl("chkSelect")).Checked) {
        int productId = ConvertType.ToInt(((HtmlInputCheckBox)row.FindControl("chkSelect")).Attributes["title"].ToString());
        ProductController.Delete(productId);
        ProductAttributesController.DeleteAllByProductId(productId);
      }
    }
    LoadSanPham();
  }
  private void LoadSanPham() {
    List<ProductInfo> list = null;
    if (cmbNhomSanPham.SelectedIndex == 0)
      list = ProductController.GetByCulture(cmbLanguage.SelectedValue);
    else
      list = ProductController.GetByCulture(cmbLanguage.SelectedValue, ToInt(cmbNhomSanPham.SelectedValue));
    if (cmbStatus.SelectedIndex > 0) {
      list = list.Where(p => p.StatusID == ConvertType.ToInt(cmbStatus.SelectedValue)).ToList();
    }
    grvSanPham.DataSource = list;
    grvSanPham.DataBind();
    if (list.Count == 0) {
      this.EditEnabled = false;
      this.DeleteEnabled = false;
    } else {
      this.EditEnabled = true;
      this.DeleteEnabled = true;
    }
  }
  protected void cmbNhomSanPham_SelectedIndexChanged(object sender, EventArgs e) {
    LoadSanPham();
  }
  protected void cmbLanguage_SelectedIndexChanged(object sender, EventArgs e) {
    LoadCategory();
    LoadSanPham();
  }
  protected void grvSanPham_SelectedIndexChanged(object sender, EventArgs e) {

  }
  protected void cmbStatus_SelectedIndexChanged(object sender, EventArgs e) {
    LoadSanPham();
  }
}
